﻿$(document).ready(function () {
    loadSoCauHoi();
    loadDanhSachCauHoi();
//    activeBox(1);
//    var time = $('#timeExpired').val();
//    var timeExpired = parseInt(time) * 60; //switch to second    
    //    $('.lb_count_down').countdown({ until: +timeExpired, format: 'MS', layout: '{mn} : {sn}', onExpiry: expired_time });
    $(".itemSmall").click(function () {
        var ItemSTT = $(this).attr("relSTT");
        activeBox(ItemSTT);
        return false;
    });
});
function lb_choose_dapan(obj) {
    var objCH = $(obj).parent().parent().parent();
    var item = $(obj).prev();
    if (!$(item).is(':checked')) {
        $(item).prop('checked', true);
    }
    else {
        var itemlenght = $(objCH).find("input:checked").length;
        if (itemlenght == 1) return false;
        $(item).prop('checked', false);
    }
    var MaCH = $(objCH).attr("relSTT");
    $("#itemSmall_" + MaCH).removeClass("label-default").addClass("label-success");
    saveDapAn(objCH);
    return true;
}
function input_choose_dapan(obj) {
    var objCH = $(obj).parent().parent().parent();
    var item = $(objCH).find("input:checked").length;
    if (item == 0) return false;
    var MaCH = $(objCH).attr("relSTT");
    $("#itemSmall_" + MaCH).removeClass("label-default").addClass("label-success");
    saveDapAn(objCH);
    return true;
}
function expired_time() {
    $('#boxHetThoiGian').modal('show');
    clearInterval(autoUplateTime);
    HoanTat();
}
function HoanTat() {
    var Data = "[";
    $(".lb_question").each(function (i, o) {
        if (i > 0) Data += ',';
        var maloai = $(this).attr("relloai");
        var cauhoi = $(this).attr("rel");
        var value = 0;
        var svalue = "";
        if (maloai != 3) {
            value = $(this).find("input:checked").val();
            if (value == undefined)
                value = 0;
            Data += "{CauHoi:"+ cauhoi + ", TraLoi:\"" + value + "\"}";
        }
        else {
            $(this).find("input:checked").each(function (j, k) {
                if (j > 0) svalue += ',';
                svalue += k.value;
            });
            Data += "{CauHoi:" + cauhoi + ", TraLoi:\"" + svalue + "\"}";
        }
    });
    Data += "]";
    $.post("ajax.aspx", { M: "HoanTat", Data: Data, DeThi: MaDe }, function (data) {
        data = $.parseJSON(data);
        if (data.Status == 'Success') {
            $('.lb_count_down').countdown('pause');
            alert("Bài làm đã được lưu, chương trình sẽ đóng lại!");
            window.location.href = 'chonde.aspx';
        }
    });
    return false;
}

function loadSoCauHoi() {
    var scauhoi ="";
    $.each(dsCauHoi, function (i, o) {
        var sLable = 'label-default';
        if (dsChon[o.MaCH] != undefined && dsChon[o.MaCH] != "") {
            sLable = 'label-success';
        }
        scauhoi += "<span relSTT='" + (i + 1) + "' class='label " + sLable + " itemSmall' id='itemSmall_" + (i + 1) + "' relDaChon='0'>Câu " + (i + 1) + "</span> ";
    });
    $("#boxSoCauHoi").html("");
    $("#boxSoCauHoi").append(scauhoi);
}

function loadDanhSachCauHoi() {
    
    var snoidung = '';
    $.each(dsCauHoi, function (i, o) {
        snoidung += '<div class="panel panel-default BoxItem" id="BoxItem_'+(i+1)+'" style="display:none">';
        snoidung += '<div class="panel-heading">';
        snoidung += '<h3 class="panel-title" id="TenCauHoi"><span>Câu hỏi ' + (i + 1) + '</span><span class="pull-right">Tổng số ' + dsCauHoi.length + ' câu</span></h3></div>';
        snoidung += '<div class="panel-body"><div class="lb_cauhoi"><p>' + o.NoiDungCH + '</p></div>';
        snoidung += '<div class="lb_question" rel="' + o.MaCH + '" relSTT="' + (i + 1) + '" relloai="' + o.MaLoai + '">';
        var luachon = [];
        if (dsChon[o.MaCH] != undefined && dsChon[o.MaCH] != "") {
            luachon = dsChon[o.MaCH].split(',');
        }
        $.each(dsDapAn, function (j, q) {
            if (q.MaCH == o.MaCH) {
                var selected = "";
                if (luachon.indexOf(q.MaDA.toString()) > -1) {
                    selected = "checked";
                }
                snoidung += '<div class="lb_q_row">';
                if (o.MaLoai != 3)
                    snoidung += '<lable><input onclick="return input_choose_dapan(this);" type="radio" '+ selected +' name="dapan_' + o.MaCH + '" value="' + q.MaDA + '" class="lb_radio itemSelect"><span onclick="return lb_choose_dapan(this);">' + q.NoiDungDA + '</span></lable>';
                else
                    snoidung += '<lable><input onclick="return input_choose_dapan(this);" type="checkbox" ' + selected + ' name="dapan_' + o.MaCH + '"value="' + q.MaDA + '" class="lb_radio itemSelect"><span onclick="return lb_choose_dapan(this);">' + q.NoiDungDA + '</span></lable>';
                snoidung += '</div>';
            }
        });
        snoidung += '</div></div>';
        snoidung += '<div class="panel-footer">';
        snoidung += '<button type="button" class="btn btn-primary" onclick="return activeBox(' + i + ');">Câu trước</button>';
        snoidung += '<button type="button" class="btn btn-success pull-right" onclick="return activeBox('+(i+2)+');">Tiếp theo</button>';
        snoidung += '</div></div>';
    });
    $("#NoiDungCauHoi").html("");
    $("#NoiDungCauHoi").append(snoidung);
}
function activeBox(id) {
    if (id <= 0) return false;
    if (id > dsCauHoi.length) {
        alert("Đây là câu hỏi cuối cùng!");
        return false;
    }
    $(".BoxItem").css("display", "none");
    $("#BoxItem_" + id).css("display", "block");
    $("#boxSoCauHoi").find(".label-danger").removeClass("label-danger");
    $("#itemSmall_" + id).addClass("label-danger");
    return false;
}

function GetBaiThi(MaBT) {
    $.post("ajax.aspx", { M: "GetBaiThi", Ma: MaBT }, function (data) {
        data = $.parseJSON(data);
        if (data.Status == 'Success') {
            
        } else {
            alert("Không thể tải đề thi!");
        }
    });
    return false;
}

function saveDapAn(objCH) {
    var maloai = $(objCH).attr("relloai");
    var cauhoi = $(objCH).attr("rel");
    var value = 0;
    var Data = "";
    var svalue = "";
    if (maloai != 3) {
        value = $(objCH).find("input:checked").val();
        if (value == undefined)
            value = 0;
        Data += "{CauHoi:"+ cauhoi + ", TraLoi:\"" + value + "\"}";
    }
    else {
        $(objCH).find("input:checked").each(function (j, k) {
            if (j > 0) svalue += ',';
            svalue += k.value;
        });
        Data += "{CauHoi:" + cauhoi + ", TraLoi:\"" + svalue + "\"}";
    }
 
    $.post("ajax.aspx", { M: "LuuDapAn", Data: Data, DeThi: MaDe }, function (data) {
        data = $.parseJSON(data);
        if (data.Status == 'Success') {
            console.log("Da luu");
        }
    });
}